package com.shop.loan.model;

import com.shop.loan.proxy.GoodsDataProxy;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.sub_erupt.Filter;
import xyz.erupt.annotation.sub_field.Edit;
import xyz.erupt.annotation.sub_field.EditType;
import xyz.erupt.annotation.sub_field.View;
import xyz.erupt.annotation.sub_field.ViewType;
import xyz.erupt.annotation.sub_field.sub_edit.*;
import xyz.erupt.upms.model.EruptUser;
import xyz.erupt.upms.model.base.HyperModel;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

@Erupt(
        name = "商品",
        filter = @Filter("Goods.isDelete = false"),
        orderBy = "Goods.bySort desc",
        dataProxy = GoodsDataProxy.class
)
@SQLDelete(sql = "update r_goods set is_delete = true, delete_time = now() where id = ?")
@Table(name = "s_goods")

@Entity
@ApiModel("商品")
@Getter
@Setter
public class Goods extends HyperModel {

    @EruptField(
            views = @View(title = "商品名称", width = "200px"),
            edit = @Edit(
                    title = "商品名称",
                    notNull = true,
                    search = @Search(vague = true),
                    placeHolder = "请输入商品名称"
            )
    )
    @ApiModelProperty(name = "name", value = "名称", dataType = "string", example = "1")
    private String name;

    @EruptField(
            views = @View(title = "商品类型", width = "60px"),
            edit = @Edit(
                    title = "商品类型",
                    type = EditType.CHOICE,
                    search = @Search,
                    notNull = true,
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "实物", value = "1"),
                                    @VL(label = "虚拟物品", value = "2"),
                            }
                    ))
    )
    private Integer goodsType;


    @EruptField(
            views = @View(title = "总库存", width = "60px")
    )
    @ApiModelProperty(name = "totalStock", value = "总库存", dataType = "int", example = "1")
    private Integer totalStock = 0;


    @EruptField(
            views = @View(title = "总出售数量", width = "60px")
    )
    @ApiModelProperty(name = "rentalStock", value = "总出售数量", dataType = "int", example = "1")
    private Integer rentalStock = 0;


    @EruptField(
            views = @View(title = "排序数值", width = "100px"),
            edit = @Edit(title = "排序数值", desc = "数值大的在前")
    )
    private Integer bySort = 0;




//    @Transient //由于该字段不需要持久化，所以使用该注解修饰
//    @EruptField(
//            edit = @Edit(title = "渠道显示配置", type = EditType.DIVIDE)
//    )
//    private String divide_loan;


//    @ManyToMany(
//            fetch = FetchType.EAGER
//    )
//    @JoinTable(
//            name = "s_product_channel",
//            joinColumns = {@JoinColumn(
//                    name = "product_id",
//                    referencedColumnName = "id"
//            )},
//            inverseJoinColumns = {@JoinColumn(
//                    name = "channel_id",
//                    referencedColumnName = "id"
//            )}
//    )
//    @OrderBy
//    @EruptField(
//            edit = @Edit(
//                    title = "渠道显示",
//                    type = EditType.CHECKBOX
//            )
//    )
//    private Set<Channel> channelLinks;
//
//
//    private Boolean channelShow;
//
//    @Transient
//    @EruptField(
//            views = {@View(
//                    title = "渠道显示", type = ViewType.SAFE_TEXT
//            )}
//    )
//    private String channelNames;


    @Transient //由于该字段不需要持久化，所以使用该注解修饰
    @EruptField(
            edit = @Edit(title = "基本信息", type = EditType.DIVIDE)
    )
    private String divide1;

    @ManyToOne
    @EruptField(
            views = @View(title = "所属类目", column = "name", width = "60px"),
            edit = @Edit(title = "所属类目", notNull = true,
                    type = EditType.REFERENCE_TREE,
                    inputType = @InputType(type = "search"),
                    referenceTreeType = @ReferenceTreeType(
                            //如果不填，展示方式为列表
                            pid = "topClassify.id")
            )
    )
    @JoinColumn(name = "classify_id")
    private Classify classify;


    @Transient //由于该字段不需要持久化，所以使用该注解修饰
    @EruptField(
            edit = @Edit(title = "详细信息", type = EditType.DIVIDE)
    )
    private String divide2;

    @EruptField(
            views = @View(title = "图片"),
            edit = @Edit(title = "图片", type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType(
                            type = AttachmentType.Type.IMAGE,
                            maxLimit = 5
                    ))
    )
    @ApiModelProperty(name = "pics", value = "商品图片  多个图片用|分割", dataType = "string", example = "1")
    private String pics;


    @Lob  //富文本编辑器所产生的文本量较大，所以设置为长字符串类型在数据库中存储
    @EruptField(
            edit = @Edit(title = "详情",
                    type = EditType.HTML_EDITOR,
                    htmlEditorType = @HtmlEditorType(HtmlEditorType.Type.UEDITOR))
    )
    @ApiModelProperty(name = "detail", value = "商品详情", dataType = "string", example = "1")
    private String detail;

    @Transient //由于该字段不需要持久化，所以使用该注解修饰
    @EruptField(
            edit = @Edit(title = "开关设置", type = EditType.DIVIDE)
    )
    private String divide3;


    @EruptField(
            views = @View(title = "是否热门"),
            edit = @Edit(
                    title = "是否热门",
                    search = @Search,
                    type = EditType.BOOLEAN,
                    notNull = true
            )
    )
    @ApiModelProperty(name = "isHot", value = "是否热门", dataType = "string", example = "1")
    private Boolean isHot = false;

    @EruptField(
            views = @View(title = "app是否显示"),
            edit = @Edit(
                    title = "app是否显示",
                    search = @Search,
                    type = EditType.BOOLEAN,
                    notNull = true
            )
    )
    @ApiModelProperty(name = "isShow", value = "app是否显示", dataType = "string", example = "1")
    private Boolean isShow = true;

    @EruptField(
            views = @View(title = "需要渠道显示"),
            edit = @Edit(
                    title = "需要渠道显示",
                    search = @Search,
                    type = EditType.BOOLEAN,
                    notNull = true
            )
    )
    private Boolean channelShow = false;

    @EruptField(
            views = @View(title = "是否包邮"),
            edit = @Edit(
                    title = "是否包邮",
                    search = @Search,
                    type = EditType.BOOLEAN,
                    notNull = true,
                    boolType = @BoolType(
                            trueText = "包邮",
                            falseText = "到付"
                    )
            )
    )
    @ApiModelProperty(name = "logistics", value = "是否包邮", dataType = "string", example = "1")
    private Boolean logistics = true;

    @Transient //由于该字段不需要持久化，所以使用该注解修饰
    @EruptField(
            edit = @Edit(title = "规格配置", type = EditType.DIVIDE)
    )
    private String divide4;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @EruptField(
            views = @View(title = "商品规格"),
            edit = @Edit(
                    title = "商品规格",
                    type = EditType.TAB_TABLE_ADD
            )
    )
    @JoinColumn(name = "goods_id")
    private Set<GoodsSize> goodsSizes;

    private Boolean isDelete = false;

    private Date deleteTime;


    public Goods(Long id) {
        setId(id);
    }

    public Goods() {
    }
}
